package com.liuyi.netty.im.test.util;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程工程实现类
 *
 * @author Mr.Fmy
 * @since 2021/12/23
 */
@Slf4j
public class ThreadFactoryImpl implements ThreadFactory {

    private final String name;

    private final AtomicInteger threadNumber = new AtomicInteger(1);

    public ThreadFactoryImpl(String name) {
        this.name = name;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setName(this.name + "-" + (threadNumber.getAndIncrement()));
        if (t.isDaemon()) {
            t.setDaemon(false);
        }
        if (t.getPriority() != Thread.NORM_PRIORITY) {
            t.setPriority(Thread.NORM_PRIORITY);
        }
        return t;
    }
}
